/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.apiGenerator.dataType.mgrInterface.resourceInfo;

import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.item.ClassGenerator;
import com.inspur.edp.caf.generator.item.ClassGeneratorContext;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.caf.generator.property.ClassPropertyGenerator;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.apiGenerator.CefApiGeneratorContext;
import com.inspur.edp.cef.generator.apiGenerator.dataType.mgrInterface.MgrInterfaceGeneratorContext;
import com.inspur.edp.cef.spi.entity.info.AbstractMgrInfo;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefModelResInfoImpl;
import java.util.List;
import org.eclipse.jdt.core.dom.Modifier;

import java.util.ArrayList;

public abstract class ModelInfoBaseGenerator extends ClassGenerator {

    private CefApiGeneratorContext parentContext;

    protected ModelInfoBaseGenerator(CefApiGeneratorContext parentContext) {
        super(parentContext.getCompilationUnitInfo());
        this.parentContext = parentContext;
    }

    //region sealed
    @Override
    protected void beforeInitialize() {
        super.beforeInitialize();
        parentContext.setMgrInfoType(new MediateType(super.compileUnit.getPackageName(), getName()));
    }

    @Override
    protected final TypeInfo setParentClass() {
        Class type = getParentClassType();
//        if (!CefModelResInfoImpl.class.isAssignableFrom(type)) {
//            throw new RuntimeException("ModelInfoBaseGenerator生成器的生成基类应为CefModelResInfoImpl及其子类。");
//        }
        return new TypeInfo(type.getName());
    }

    @Override
    protected final String getName() {
        final String itemNameTemplate = "%1$sModelResourceInfo";
        return String.format(itemNameTemplate, getModelCode());
    }

    @Override
    protected final ClassGeneratorContext createClassInfoContext() {
        return new ClassGeneratorContext();
    }

    @Override
    protected final java.util.ArrayList<Modifier.ModifierKeyword> getAccessModifier() {
        return new java.util.ArrayList<>(
                java.util.Arrays.asList(new Modifier.ModifierKeyword[]{Modifier.ModifierKeyword.PUBLIC_KEYWORD}));
    }

    @Override
    protected ArrayList<ClassMethodGenerator> createConstructorGenerators() {
        ArrayList<ClassMethodGenerator> ctors = super.createConstructorGenerators();
        if (ctors == null)
            ctors = new ArrayList<>();
        ctors.add(getModelInfoCtorGenerator());
        return ctors;
    }

  protected ModelInfoCtorGenerator getModelInfoCtorGenerator() {
    return new ModelInfoCtorGenerator(getName(), getMetaCurrentSU(), getMetaResourceMetaId(),getMetaDataId(), getModelRootNodeCode(), getModelDisplayValueKey(), parentContext);
  }

  //endregion

    //region 私有
    protected String getMetaResourceMetaId() {
        return parentContext.getResourceMetadataId();
    }

    protected String getMetaDataId() {
        return parentContext.getCefGenContext().getMetadata().getHeader().getId();
    }

    protected String getMetaCurrentSU() {
        return parentContext.getProjectServiceUnit();
    }

    //endregion

    protected Class getParentClassType() {
        return CefModelResInfoImpl.class;
    }

    protected abstract String getModelCode();

    protected abstract String getModelDisplayValueKey();

    protected abstract String getModelRootNodeCode();
    @Override
    protected  ArrayList<ClassMethodGenerator> createMethodGenerators() {
        ArrayList<ClassMethodGenerator> methods = super.createMethodGenerators();
        if (methods == null) {
            methods = new ArrayList<>();
        }
        createExtendMethodGenerators(methods);
        return methods;
    }

    protected void createExtendMethodGenerators(List<ClassMethodGenerator> methods){}
}
